package com.tarena.lbs.user.controller;

import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.api.UserApi;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户人群业务相关
 */
@Tag(name = "用户人群管理")
@RestController
public class UserGroupController {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UserGroupController.class);
    @Autowired
    private UserGroupService userGroupService;
    @DubboReference
    private UserApi userApi;


    @Operation(summary = "人群数据分页列表")
    @PostMapping("/admin/basic/userGroup/list")
        public Result<PageResult<UserGroupVO>> pageList(@RequestBody UserGroupQuery query)
            throws BusinessException{
        Integer businessId = getLoginId();
        if(businessId!=null && businessId>0){
            query.setBusinessId(businessId);
        }
             PageResult<UserGroupVO> page = userGroupService.pageList(query);
            return new Result<>(page);
    }
    @Operation(summary = "人群详情")
    @GetMapping("/admin/basic/userGroup/detail")
    public Result<UserGroupVO> detail(@RequestParam Integer id)
            throws BusinessException {
        if (id == null || id <= 0) {
            throw new BusinessException("-1", "人群ID不能为空或无效");
        }

        UserGroupVO vo = userGroupService.detail(id);
        return new Result<>(vo);
    }



    @Operation(summary = "保存人群数据")
    @PostMapping("/admin/basic/userGroup/save")
    public Result<Void> save(@RequestBody UserGroupParam param)
            throws BusinessException {

        Integer businessId = getLoginId();
        if(businessId!=null && businessId>0){
            param.setBusinessId(businessId);
        }
        userGroupService.save(param);
        return Result.success();
    }
    @Operation(summary = "更新人群数据")
    @PostMapping("/admin/basic/userGroup/update")
    public Result<Void> update(@RequestBody UserGroupParam param)
            throws BusinessException {
        log.debug("更新人群数据，入参:{}", param);
        userGroupService.update(param);
        log.debug("更新人群数据成功，ID:{}", param.getId());
        return Result.success();
    }
    @Operation(summary = "更新人群状态")
    @PostMapping("/admin/basic/userGroup/changeStatus")
    public Result<Void> changeStatus(@RequestBody UserGroupParam param)
            throws BusinessException {

        Integer businessId = getLoginId();
        if(businessId!=null && businessId>0){
            param.setBusinessId(businessId);
        }
        userGroupService.changeStatus(param.getId(), param.getStatus());
        return Result.success();
    }
    @Operation(summary = "删除人群")
    @PostMapping("/admin/basic/userGroup/delete")
    public Result<Void> delete(@RequestBody UserGroupParam param)
            throws BusinessException {

        Integer businessId = getLoginId();
        if(businessId!=null && businessId>0){
            param.setBusinessId(businessId);
        }
        userGroupService.delete(param.getId());
        return Result.success();
    }
    @Operation(summary = "根据id 查询人群姓名活动使用")
    @PostMapping("/admin/basic/userGroup/getUserById")
    public Result<List<UserGroupVO>> userActivatyList(@RequestBody UserGroupQuery query)
            throws BusinessException {
        List<UserGroupVO> list = userGroupService.userActivatyList(query);
        return new Result<>(list);
    }


    private Integer getLoginId() throws BusinessException {
        // 1. 获取当前用户认证信息
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        // 2. 检查用户是否已认证
        Asserts.isTrue(userPrinciple == null, new BusinessException("-2", "用户认证解析失败"));
        AdminDto adminDto = new AdminDto();
        return adminDto.getBusinessId();
    }
}